package com.eshop.we.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eshop.we.business.CategoryBusiness;
import com.eshop.we.category.entity.Category;
import com.eshop.we.result.Result;
import com.eshop.we.user.dto.UserInfoDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    CategoryBusiness categoryBusiness;


    @GetMapping("/getcategorypage")
    public Result  getCategoryPage(@RequestParam(required = false)String title,
                                   @RequestParam(required = false,defaultValue = "1")Integer pageNum, @RequestParam(required = false,defaultValue = "20")Integer pageSize){

        Category category=new Category();
        category.setTitle(title);
        EntityWrapper ew= new EntityWrapper<Category>();
        ew.setEntity(category);
        Page<Category> page=new Page<>(pageNum,pageSize);
        page=categoryBusiness.selectPage(page,ew);
        return  Result.success(page);
    }

    @PostMapping("/addcategory")
    public Result addCategory(@RequestParam(required = true)String title,@RequestParam(required = true)Integer sort,
                              @RequestParam(required = true)String describe,@RequestParam(required =true )Integer status){

        Category category=new Category();
        category.setTitle(title);
        category.setStatus(status);
        category.setDescribe(describe);
        category.setSort(sort);
        category.setCtime((int)(System.currentTimeMillis()/1000));
        return  Result.success(categoryBusiness.insert(category));
    }

    @PostMapping("/updatecategory")
    public Result updateCategory(@RequestParam(required = true)Integer categoryId,@RequestParam(required = false)String title,
                                 @RequestParam(required = false)Integer sort,@RequestParam(required = false)String describe,
                                 @RequestParam(required =false )Integer status){
        Category category=new Category();
        category.setId(categoryId);
        if(StringUtils.isNotBlank(title)){ category.setTitle(title); }
        if(sort!=null){ category.setSort(sort); }
        if(StringUtils.isNotBlank(describe)){category.setDescribe(describe);}
        if(status!=null){category.setStatus(status);}
        return  Result.success(categoryBusiness.updateById(category));
    }

    @GetMapping("/getcategoryinfo")
    public Result getCategoryInfo(@RequestParam(required = true)Integer categoryId){
        Category category=new Category();
        category.setId(categoryId);
        return Result.success(categoryBusiness.selectById(category));
    }
}
